package com.jpc.musicdelightapplication.ui.square

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.jpc.lib_base.ext.getEmptyView
import com.jpc.lib_base.ext.initColors
import com.jpc.lib_base.utils.ScreenUtil
import com.jpc.musicdelightapplication.R
import com.jpc.musicdelightapplication.base.BaseFragment
import com.jpc.musicdelightapplication.data.bean.PlaylistData
import com.jpc.musicdelightapplication.databinding.IncludeSwiperefreshRecyclerviewBinding
import com.jpc.musicdelightapplication.ui.square.SquareChildViewModel.Companion.LIMIT
import com.jpc.lib_base.BaseApplication.Companion.baseContext
import com.jpc.lib_base.utils.LogUtil

private const val TAG = "SquareChildFragment"
class SquareChildFragment :
    BaseFragment<SquareChildViewModel, IncludeSwiperefreshRecyclerviewBinding>(
        R.layout.include_swiperefresh_recyclerview
    ) {

    companion object {
        private const val TAB_TITLE = "tab_title"

        fun newInstance(tabTitle: String = "全部"): Fragment {
            return SquareChildFragment().apply {
                arguments = Bundle().apply {
                    putString(TAB_TITLE, tabTitle)
                }
            }
        }
    }
    private var itemWidth = 0
    init {
        itemWidth = (ScreenUtil.getScreenWidth(baseContext) - ScreenUtil.dp2px(baseContext, 52f)) / 3
    }

    private val mAdapter by lazy { ItemPlaylistAdapter(itemWidth) }
    //private val mAdapter = ItemPlaylistAdapter(itemWidth)
    private var mPageNo: Int = 1

    override fun initView() {
        mBinding.apply {
            recyclerView.apply {
                layoutManager = GridLayoutManager(requireContext(), 3) //LinearLayoutManager(requireContext())//
                adapter = mAdapter.apply {
                    loadMoreModule.setOnLoadMoreListener { loadMoreData() }
                    // 添加可点击项
                    //addChildClickViewIds()
                    // 可点击项的点击事件
                    //setOnItemChildClickListener{_, view, position -> }
                }
                swipeRefreshLayout.apply {
                    initColors()
                    setOnRefreshListener { onRefresh() }
                }
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        onRefresh()
    }

    override fun createObserve() {
        super.createObserve()
        mViewModel.apply {
            playlistLiveData.observe(viewLifecycleOwner){
                it?.let { handlePlaylistData(it) }
            }
        }
    }

    // 处理分页数据
    private fun handlePlaylistData(list: List<PlaylistData>){
        mAdapter.apply {
            if (mPageNo == 1){
                if (list.isEmpty()){
                    setEmptyView(recyclerView.getEmptyView())
                }
                setList(list)
            }else{
                addData(list)
            }
            loadMoreModule.apply {
                isEnableLoadMore = true
                if (list.size < LIMIT){
                    loadMoreEnd() // 不满一页或没有更多数据
                }else{
                    loadMoreComplete()
                }
            }
            mBinding.swipeRefreshLayout.isEnabled = true
        }
        mBinding.swipeRefreshLayout.isRefreshing = false
    }

    // 根据cat分类获取歌单
    private fun fetchPlaylistPageList(page: Int = 1) {
        arguments?.getString(TAB_TITLE)?.let { mViewModel.fetchPlaylist(it, (page - 1) * LIMIT) }
    }

    // 下拉刷新
    private fun onRefresh() {
        mBinding.swipeRefreshLayout.isRefreshing = true
        mAdapter.loadMoreModule.isEnableLoadMore = false
        fetchPlaylistPageList()
    }

    // 上拉加载
    private fun loadMoreData(){
        mBinding.swipeRefreshLayout.isEnabled = false
        fetchPlaylistPageList(++mPageNo)
    }
}